package org.jeecg.config;

import org.jeecg.common.constant.RabbitMqConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMq配置
 *
 * @author ZhangYu
 * @date 2023/9/24 23:45
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 配置队列
     * <p>
     * true：表示持久化 (不填，默认为true,默认持久化)
     * durable：队列是否持久化。 队列默认是存放到内存中的，rabbitmq 重启则丢失，
     * 若想重启之后还存在则队列要持久化，
     * 保存到 Erlang 自带的 Mnesia 数据库中，当 rabbitmq 重启之后会读取该数据库
     */
    @Bean
    public Queue testQueueOne() {
        return new Queue(RabbitMqConstant.TEST_QUEUE_ONE);
    }

    @Bean
    public Queue testQueueTwo() {
        return new Queue(RabbitMqConstant.TEST_QUEUE_TWO);
    }

    //----------------------------------------------------fanout广播模式--------------------------------------------------

    /**
     * 创建交换机
     *
     * @return 交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(RabbitMqConstant.FANOUT_EXCHANGE);
    }

    /**
     * 将队列和交换机进行绑定
     *
     * @return 队列和交换机进行绑定
     */
    @Bean
    public Binding fanoutBindingOne() {
        return BindingBuilder.bind(testQueueOne()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBindingTwo() {
        return BindingBuilder.bind(testQueueTwo()).to(fanoutExchange());
    }

    //---------------------------------------------------------direct路由模式---------------------------------------------

    /**
     * 路由交换机
     *
     * @return 路由交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitMqConstant.DIRECT_EXCHANGE);
    }

    /**
     * 将队列和交换机进行绑定，并给队列绑定路由
     *
     * @return 绑定路由
     */
    @Bean
    public Binding directBindingOne() {
        return BindingBuilder.bind(testQueueOne()).to(directExchange()).with(RabbitMqConstant.ROUTING_ONE);
    }

    @Bean
    public Binding directBindingTwo() {
        return BindingBuilder.bind(testQueueTwo()).to(directExchange()).with(RabbitMqConstant.ROUTING_TWO);
    }

    //---------------------------------------------------topic主题模式----------------------------------------------------

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitMqConstant.TOPIC_EXCHANGE);
    }

    @Bean
    public Binding topicBindingOne() {
        return BindingBuilder.bind(testQueueOne()).to(topicExchange()).with(RabbitMqConstant.TOPIC_ROUTING_ONE);
    }

    @Bean
    public Binding topicBindingTwo() {
        return BindingBuilder.bind(testQueueTwo()).to(topicExchange()).with(RabbitMqConstant.TOPIC_ROUTING_TWO);
    }

}
